<html>

<head>
<title>Reactive Desktop</title>
</head>

<body bgcolor="#FFFFFF">
<div style="position: absolute; overflow: scroll; overflow-x : hidden; width: 500px; height: 300px; z-index: 1; left: 150; top: 150px; background:#CCCCCC;" id="console">
	<p align="center"><font color="red" face="courier">Reactive Desktop<br></font></p>
</div>

<script language="javascript"  type="text/javascript">
window.onerror = ClearError;

window.setInterval("callNextInQueue();", 2000);

var XMLRequestObject = null;

var UpdateRequests = new Array();

var operation_completed = true;

var logged_in = false;

var SystemBackend = "http://localhost:5050/local/op=do&login";
var SystemCallBackend = "http://localhost:5050/local/";

var toAdd = "";

window.setInterval("makeNewRequest(\"GET\", SystemCallBackend + \"op=get&calls\", checkSystemCalls);", 15000);

var retry_login_timer = null;

var module_config = new Array();

run_login();

makeNewRequest("GET", SystemCallBackend + "op=get&calls", checkSystemCalls);


function ClearError(msg, url, line) 
{
	add_to_console("---------------------------------------------");
	add_to_console("ERROR: Segmentation fault - Core dump");
	add_to_console(msg);
	add_to_console("FILE: " + url);
	add_to_console("Line: #" + line);
	return true;
}


function add_to_console(to_report)
{
	var con = document.getElementById("console");
	//if(con.style.visibility == "visible")
	//{
		var temp = document.getElementById("console").innerHTML;
		temp = temp.toString();
		
		if(temp.length > 2048)
		{
			document.getElementById("console").innerHTML = "";
		}
		
		document.getElementById("console").innerHTML+= "<font color=\"white\" aling=\"left\" face=\"courier\">" + to_report + "</font><br>";
		document.getElementById("console").scrollTop = document.getElementById("console").scrollHeight;	
	//}
}

function makeNewRequest(type, url, function_to_call)
{
 if(operation_completed == false)
 {
	addToQueue(type, url, function_to_call);
	return(true);
 }
 
 operation_completed = false;
 add_to_console("root@home: ./update " + url);
 if (window.XMLHttpRequest) // try to create XMLHttpRequest
 {
  XMLRequestObject = new XMLHttpRequest();
 }
 
 if (window.ActiveXObject)	// if ActiveXObject use the Microsoft.XMLHTTP
 {
  XMLRequestObject = new ActiveXObject("Microsoft.XMLHTTP");
 }
 XMLRequestObject.open(type, url , true);
 // Set the onreadystatechange function
 XMLRequestObject.onreadystatechange = function () 
 {
	reset_for_request(function_to_call);
 }

 // Send
 XMLRequestObject.send(null); 
 //add_to_console("Request sent to remote server");
}

function reset_for_request(function_name)
{
	if(XMLRequestObject.readyState == 4)
	{
		add_to_console("Date recieved, attempting to parse");
		operation_completed = true;
		function_name();
		if(UpdateRequests.length > 0)
		{
			callNextInQueue();
		}
	}
}

function addToQueue(type, url, function_to_call)
{
	var temp = new Array();
	temp['type'] = type;
	temp['url'] = url;
	temp['call'] = function_to_call;
	
	var test = new Array();
	for(var i = 0; i < UpdateRequests.length; i++)
	{
		test = UpdateRequests[i];
		if(test['call'] == function_to_call)
		{
			return(0);
		}
	}
	UpdateRequests.push(temp);
	add_to_console("%%%%% Added one to the list");
}

function callNextInQueue()
{
	if(UpdateRequests.length > 0)
	{
		var temp = UpdateRequests.shift();
		type = temp['type'];
		url = temp['url'];
		function_to_call = temp['call'];
		add_to_console("****** Removed one from the list");
		add_to_console(url);
		makeNewRequest(type, url, function_to_call);
	}
}


function run_login()
{
	if(logged_in == false)
	{
		add_to_console("root@home: Initalising the system");
		makeNewRequest("GET", SystemBackend, SystemLoginUpdateReq);
		add_to_console("root@home: Attempting to log in");	
	}
	else
	{
		retry_login_timer = null;
	}
}


function SystemLoginUpdateReq() 
{
 // If data received correctly
	logged_in = false;
		if (XMLRequestObject.readyState == 4) 
	{	
		//alert(XMLRequestObject.responseText);
		if (XMLRequestObject.responseText.indexOf('invalid') == -1) 
		{ 	
			try
			{
				var node = XMLRequestObject.responseXML.documentElement; 
			   // Get Channel information
				var user = node.getElementsByTagName('user');
				var modules = node.getElementsByTagName('modules');
				
				var username = user[0].getElementsByTagName('username').item(0).firstChild.data;
				var status = user[0].getElementsByTagName('status').item(0).firstChild.data;
				if(status != "logged_in")
				{
					add_to_console("root@home: There was an error logging you in");
				}
				else
				{
					add_to_console("Logged In");
					add_to_console("Loading All Modules . . .");
					var mod_name = "";
					var mod_file = "";
					var mod_right = "";
					var mod_left = "";
					var mod_top = "";
					var mod_refresh = "";
					var toAdd = "";
					
					module_config = new Array(modules.length - 1);
					
					for(i = 0; i < modules.length; i++)
					{
						mod_name = modules[i].getElementsByTagName('name').item(0).firstChild.data;
						mod_file = modules[i].getElementsByTagName('file').item(0).firstChild.data;
						var configs = modules[i].getElementsByTagName('config');
						if(configs.length > 0)
						{
							var temp = new Array();
							for(j = 0; j < configs.length; j++)
							{
								var config_value = configs.item(j).firstChild.data;
								var config_name = configs.item(j).attributes[0].nodeValue;
								temp[""+config_name] = config_value;
							}
							module_config[mod_name] = temp;
						}

						add_to_console("root@home: ./"+mod_file);				
						var e = document.createElement("script");
						e.src = "modules\\" + mod_file;
						e.type="text\/javascript";
						document.getElementsByTagName("head")[0].appendChild(e); 
						add_to_console("Sucessfuly loaded: " + mod_name);
					}
					changeConsoleVisibility(true);
					window.clearInterval(retry_login_timer);
					logged_in = true;
					callNextInQueue();
					
				}
				node = null;
				user = null;
				modules = null;
			}
			catch(err)
			{
				add_to_console("root@home: There was an error logging you in");
				retry_login_timer = window.setInterval("run_login();",10000);
			}
		}
		else 
		{
			add_to_console("Error Handling recieved log in information");
		}
	}
}

function checkSystemCalls() 
{
 // If data received correctly
	if (XMLRequestObject.readyState == 4) 
	{	
		if (XMLRequestObject.responseText.indexOf('invalid') == -1) 
		{ 	
			var node = XMLRequestObject.responseXML.documentElement; 
		    // Get Channel information
			var calls = node.getElementsByTagName('call');
			
			add_to_console("Yay, something to update");
			for(var i = 0; i < calls.length; i++)
			{
				var op = calls[i].getElementsByTagName('op').item(0).firstChild.data;
				var todo = calls[i].getElementsByTagName('todo').item(0).firstChild.data;
				eval(todo);
			}
		}
		else 
		{
			add_to_console("Error Handling recieved log in information");
		}
	}
}

function changeConsoleVisibility(hide)
{
	var con = document.getElementById("console");
	if(hide == true)
	{
		con.style.visibility = "hidden";
	}
	else
	{
		con.style.visibility = "visible";
	}
}

function getAbsoluteOffsetTop(obj) {var top = obj.offsetTop;var parent = obj.offsetParent;while (parent != document.body) {top += parent.offsetTop;parent = parent.offsetParent;}return top;}
function getAbsoluteOffsetLeft(obj) {var left = obj.offsetLeft;	var parent = obj.offsetParent; while (parent != document.body) {left +=parent.offsetLeft;parent = parent.offsetParent;}	return left;}
</script>

</script>
</body>



</html>
